package cn.com.bluemoon.daps.model.service.impl;

import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.common.enums.BmStatus;
import cn.com.bluemoon.daps.common.enums.DatabaseType;
import cn.com.bluemoon.daps.model.dto.DapDataModelTableDto;
import cn.com.bluemoon.daps.model.dto.score.TableScoreDto;
import cn.com.bluemoon.daps.model.entity.DapDataModelByteLength;
import cn.com.bluemoon.daps.model.entity.DapDataModelTablePartition;
import cn.com.bluemoon.daps.model.entity.DapDataModelTableScoreRule;
import cn.com.bluemoon.daps.model.mapper.DapDataModelTablePartitionMapper;
import cn.com.bluemoon.daps.model.mapper.DapDataModelTableScoreRuleMapper;
import cn.com.bluemoon.daps.model.service.DapDataModelByteLengthService;
import cn.com.bluemoon.daps.model.service.DapDataModelTablePartitionService;
import cn.com.bluemoon.daps.model.service.DapDataModelTableScoreRuleService;
import cn.com.bluemoon.daps.model.utils.score.TableStructureScore;
import cn.com.bluemoon.daps.model.vo.score.TableScoreRuleCode;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class DapDataModelTableScoreRuleServiceImpl extends ServiceImpl<DapDataModelTableScoreRuleMapper, DapDataModelTableScoreRule> implements DapDataModelTableScoreRuleService {

    @Resource
    DapDataModelByteLengthService dapDataModelByteLengthService;

    /**
     * 表得分详情
     * @param list
     */
    @Override
    public List<TableScoreDto> getTableScore(List<DapDataModelTableDto> list, List<TableScoreDto> dtoList) {

        // 获得生效的评分规则列表
        List<DapDataModelTableScoreRule> ruleList = this.getBaseMapper().selectList(new LambdaQueryWrapper<DapDataModelTableScoreRule>()
                .eq(DapDataModelTableScoreRule::getBmStatus, BmStatus.ON.getCode()).eq(DapDataModelTableScoreRule::getIsEffect, 1));
        // 查询
        List<DapDataModelTableScoreRule> collect = ruleList.stream().filter(r -> r.getRuleCode().equals(TableScoreRuleCode.SINGLE_MAX_LENGTH)).collect(Collectors.toList());
        List<DapDataModelByteLength> lengthList = null;
        if(!collect.isEmpty()){
            lengthList = dapDataModelByteLengthService.getBaseMapper().selectList(new LambdaQueryWrapper<DapDataModelByteLength>()
                    .eq(DapDataModelByteLength::getDatabaseType, DatabaseType.MYSQL.getCode())
                    .eq(DapDataModelByteLength::getBmStatus, BmStatus.ON.getCode()));
        }
        // 封装表分数
        List<TableScoreDto> scoreDtoList = Lists.newArrayList();
        for (DapDataModelTableDto dto : list){
            double score = 0;
            ResultBean<Double> tableScore = TableStructureScore.getTableScore(ruleList, dto, lengthList);
            if(tableScore.getCode() == 200){
                score = tableScore.getContent();
            }
            // 为空表示申请发布调用
            if(dtoList == null){
                TableScoreDto tableScoreDto = new TableScoreDto();
                tableScoreDto.setTableScore(score);
                scoreDtoList.add(tableScoreDto);
            }else {
                TableScoreDto tableScoreDto = dtoList.stream().filter(d -> d.getTableId().equals(dto.getTableId())).findFirst().get();
                tableScoreDto.setTableScore(score);
            }
        }
        return dtoList == null ? scoreDtoList : dtoList;
    }

    @Override
    public List<DapDataModelTableScoreRule> singleTableScore(DapDataModelTableDto dto) {

        // 获得生效的评分规则列表
        List<DapDataModelTableScoreRule> ruleList = this.getBaseMapper().selectList(new LambdaQueryWrapper<DapDataModelTableScoreRule>()
                .eq(DapDataModelTableScoreRule::getBmStatus, BmStatus.ON.getCode()).eq(DapDataModelTableScoreRule::getIsEffect, 1));
        // 查询是否存在
        List<DapDataModelTableScoreRule> collect = ruleList.stream().filter(r -> r.getRuleCode().equals(TableScoreRuleCode.SINGLE_MAX_LENGTH)).collect(Collectors.toList());
        List<DapDataModelByteLength> lengthList = null;
        if(!collect.isEmpty()){
            lengthList = dapDataModelByteLengthService.getBaseMapper().selectList(new LambdaQueryWrapper<DapDataModelByteLength>()
                    .eq(DapDataModelByteLength::getDatabaseType, DatabaseType.MYSQL.getCode())
                    .eq(DapDataModelByteLength::getBmStatus, BmStatus.ON.getCode()));
        }
        // 封装表分数
        List<DapDataModelTableScoreRule> rules = TableStructureScore.singleTable(ruleList, dto, lengthList);
        return rules;
    }
}
